package cn.stu.projectsummit.user.service.impl;


import cn.stu.projectsummit.base.exception.ServiceException;
import cn.stu.projectsummit.base.response.JsonResult;
import cn.stu.projectsummit.base.response.StatusCode;
import cn.stu.projectsummit.school.mapper.SchoolMapper;
import cn.stu.projectsummit.school.pojo.dto.SchoolSaveParam;
import cn.stu.projectsummit.user.mapper.UserMapper;

import cn.stu.projectsummit.user.pojo.dto.UserLoginParam;
import cn.stu.projectsummit.user.pojo.dto.UserQuery;
import cn.stu.projectsummit.user.pojo.dto.UserSaveParam;
import cn.stu.projectsummit.user.pojo.entity.User;
import cn.stu.projectsummit.user.pojo.vo.UserVO;
import cn.stu.projectsummit.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    SchoolMapper schoolMapper;


    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("用户查询业务:userQuery={}", userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        //根据每个user的schoolId,将UserVO的schoolName赋值
        list.forEach(userVO -> {
            userVO.setSchoolName(schoolMapper.selectSchoolNameById(userVO.getSchoolId()));
        });
        return list;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("用户保存业务:userSaveParam={}", userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        //如果没有id值,说明是新增操作
        if (user.getId() == null) {
            //为用户设置初始密码
            user.setPassword("123456");
            //为用户设置注册时间
            user.setCreateTime(new Date());

            //若用户的schoolName不存在于school中，则向school表中插入schoolName
            if (!schoolMapper.selectSchoolByName(userSaveParam.getSchoolName())) {
                SchoolSaveParam schoolSaveParam = new SchoolSaveParam();
                schoolSaveParam.setId(user.getSchoolId());
                schoolSaveParam.setSchoolName(userSaveParam.getSchoolName());
                schoolSaveParam.setCity(userSaveParam.getCity());
                log.debug("新增学校业务:schoolSaveParam={}", schoolSaveParam);
                schoolMapper.insert(schoolSaveParam);
            }
            //查询到schoolId,将user的schoolId赋值
            user.setSchoolId(schoolMapper.selectSchoolIdByName(userSaveParam.getSchoolName()));
            //向用户表中插入数据
            userMapper.insert(user);
        } else {//如果有id说明是编辑用户操作,走更新方法
            log.debug("编辑用户业务:user={}", user);
            user.setUpdateTime(new Date());

            //若用户的schoolName不存在于school中，则向school表中插入schoolName
            if (!schoolMapper.selectSchoolByName(userSaveParam.getSchoolName())) {
                SchoolSaveParam schoolSaveParam = new SchoolSaveParam();
                schoolSaveParam.setId(user.getSchoolId());
                schoolSaveParam.setSchoolName(userSaveParam.getSchoolName());
                schoolSaveParam.setCity(userSaveParam.getCity());
                log.debug("新增学校业务:schoolSaveParam={}", schoolSaveParam);
                schoolMapper.insert(schoolSaveParam);
            }
            //查询到schoolId,将user的schoolId赋值
            user.setSchoolId(schoolMapper.selectSchoolIdByName(userSaveParam.getSchoolName()));
            userMapper.update(user);
        }
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码业务:userId={}", userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户业务:userId={}",userId);
        userMapper.deleteById(userId);
    }

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录业务,参数:{}",userLoginParam);
        UserVO userVO = userMapper.selectByUserName(userLoginParam.getUsername());
        //根据用户输入的用户名,查不到用户信息:用户名错误
        if(userVO==null){
            //业务层不能直接响应结果给前端,业务层遇到问题需要抛出业务层异常!
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        //根据用户名查到的正确密码与用户输入的密码不一致:密码错误
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //如果没有抛出任何异常,说明用户名与密码均正确,登录成功!
        return userVO;
    }




}
